package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired(required = false)
    private DishService dishService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session) {
        Long id = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(id);
        dishDto.setCreateUser(id);

        dishService.save(dishDto);
        return R.success("添加成功");
    }


    @GetMapping("/page")
    public R<Page<DishDto>> findByPage(Integer page,Integer pageSize,String name){
      //  Page<DishDto> pageObj = dishService.findByPage2(page, pageSize, name);
        Page<DishDto> pageObj = dishService.findByPage1(page, pageSize, name);
        return R.success(pageObj);
    }


    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        return dishService.updateById(dishDto);
    }


    @PostMapping("/status/{status}")
    public R<String> updateStatus(Long[] ids,@PathVariable Integer status) {
        Boolean flag = dishService.updateStatedis(ids,status);
       if(!flag){
           return R.error("修改失败");
       }
        return R.success("修改成功");
    }


//    @PostMapping("/status/1")
//    public R<String> updateStatus1(Long[] ids) {
//     /*   for (Long id : ids) {
//            System.out.println("看这里："+id);
//        }*/
//        Boolean flag = dishService.updateStateable(ids);
//        if(!flag){
//            return R.error("修改失败");
//        }
//        return R.success("修改成功");
//    }

    @DeleteMapping
    public R<String> delete(Long[] ids) {
     /*   for (Long id : ids) {
            System.out.println("看这里："+id);
        }*/
        Boolean flag = dishService.delete(ids);
        if(!flag){
            return R.error("删除失败");
        }
        return R.success("删除成功");
    }

/*
    @RequestMapping("list")
    public R<List<Dish>> findByCategoryId(Long categoryId){
        List<Dish> dishList = dishService.findByCategoryId(categoryId);
        return R.success(dishList);
    }
*/

    @RequestMapping("/list")
    public R<List<DishDto>> findByCategoryId(Long categoryId,Integer status,String name){
        List<DishDto> dishList = dishService.findByCategoryId(categoryId, status,name);
        return R.success(dishList);
    }
}
